<?php
// +----------------------------------------------------------------------
// | Fanwe 多语商城建站系统 (Build on ThinkPHP)
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://www.fanwe.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: awfigq(67877605@qq.com)
// +----------------------------------------------------------------------

// 导航
class SiteAction extends CommonAction
{
	//列表
	public function index()
	{
		$cate_id = intval($_REQUEST['cate_id']);
		$keywords = trim($_REQUEST['keywords']);
		$status = trim($_REQUEST['status']);
		$is_collect = trim($_REQUEST['is_collect']);

		if(!empty($cate_id))
		{
			$this->assign("cate_id",$cate_id);
			$where.=" and s.cate_id = $cate_id";
			$parameter .= "cate_id=" . $cate_id."&";
		}

		if(!empty($keywords))
		{
			$this->assign("keywords",$keywords);
			$where.=" and (s.name like '%$keywords%' or s.url like '%$keywords%' or s.site_key like '%$keywords%')";
			$parameter .= "keywords=" . urlencode($keywords)."&";
		}

		if($status != "" && $status >= 0)
		{
			$where.=" and s.status = $status";
			$parameter .= "status=" . $status ."&";
			$this->assign("status",$status);
		}
		else
		{
			$this->assign("status",-1);
		}

		if($is_collect != "" && $is_collect >= 0)
		{
			if($is_collect == 1)
				$where.=" and sr.rule <> ''";
			else
				$where.=" and sr.rule is null";

			$parameter .= "is_collect=" . $is_collect ."&";
			$this->assign("is_collect",$is_collect);
		}
		else
		{
			$this->assign("is_collect",-1);
		}

		$sql_str = 'SELECT s.id,s.is_auth,is_all,s.name,s.site_key,s.close_collect,s.url,s.status,s.sort,s.cate_id,sc.name as scname,s.is_best '.
					'FROM '.C("DB_PREFIX").'site as s '.
					'left join '.C("DB_PREFIX").'site_cate as sc on sc.id = s.cate_id '.
					"left join ".C("DB_PREFIX")."site_rules as sr on sr.id = s.id and sr.name = 'collect_url' ".
					"where s.id is not null $where group by s.id";

		$model = M();
        $voList = $this->_Sql_list($model, $sql_str, "&".$parameter, 'id', false);

		$cateList = D("SiteCate")->getSiteCateList();
		$this->assign("siteCates",$cateList);

		$this->display ();
		return;
	}

	public function add()
	{
		//导航分类
		$cateList = D("SiteCate")->getSiteCateList();
		$this->assign("cateList",$cateList);

		$citys = D("GroupCity")->where("status = 1")->order("sort asc,id desc,py asc")->findAll();
		$this->assign("citys",$citys);

		$new_sort = D("Site")->where("status=1") -> max("sort") + 1;
		$this->assign("new_sort",$new_sort);

		$ruleType = D("CollectRuleType")->where("status = 1")->order("sort asc,id desc")->findAll();
		$this->assign("ruleType",$ruleType);

		$this->getGroupCityList_by_py(0);

		$this->display();
	}

	public function insert()
	{
		$model = D(MODULE_NAME);

		$_POST['content'] = addslashes($_POST['content']);
		$_POST['is_all'] = isset($_POST['is_all']) ? intval($_POST['is_all']) : 0;

		if (false === $model->create())
		{
			$this->error($model->getError());
		}
		//保存当前数据对象
		$id = $model->add();
		if ($id !== false)
		{
			$upload_list = $this->uploadFile(1,"site");
			if($upload_list)
			{
				D("Site")->where("id=$id")->setField("site_img",$upload_list[0]['recpath'].$upload_list[0]['savename']);
			}

			$data = array();
			$data['collect_url'] = trim($_POST['collect_url']);
			$data['collect_code'] = trim($_POST['collect_code']);
			$data['collect_type'] = intval($_POST['collect_type']);
			$data['web_type'] = intval($_POST['web_type']);
			$data['collect_city_api'] = trim($_POST['collect_city_api']);

			if($data['collect_type'] == 1)
			{
				foreach($_POST['web'] as $key => $val)
				{
					$data[$key] = $val;
				}
			}
			else
			{
				foreach($_POST['api'] as $key => $val)
				{
					$data[$key] = $val;
				}
			}

			foreach($data as $key => $value)
			{
				if(!empty($value))
				{
					$site_rules = array();
					$site_rules['id'] = $id;
					$site_rules['name'] = $key;
					$site_rules['rule'] = $value;
					D("SiteRules")->add($site_rules);
				}
			}

			$city_site=$_POST['city_site'];
			foreach($city_site as $k=>$v)
			{
				$city_site_list=array();
				$city_site_list['site_id']=$id;
				$city_site_list['city_id']=$k;
				$city_site_list['sort']=$v;

				if(is_numeric($v))
				{
					D('CitySite')->add($city_site_list);
				}
			}

			$this->saveLog(1, $id);
			$this->success(L('ADD_SUCCESS'));
		}
		else
		{
			//失败提示
			$this->saveLog(0, $id);
			$this->error(L('ADD_FAILED'));
		}
	}

	public function edit()
	{
		$model = M(MODULE_NAME);

		$id = $_REQUEST[$model->getPk()];
		$vo = $model->getById($id);
		$vo['content'] = stripslashes($vo['content']);
		$this->assign('vo', $vo);

		$siteRules = D("SiteRules")->where("id=$id")->getField("name,rule");

		foreach($siteRules as $key => $value)
		{
			$siteRules[$key] = stripslashes($value);
		}
		$this->assign('siteRules', $siteRules);

		$this->getGroupCityList_by_py($id);

		if($vo['user_id'] > 0)
		{
			$user = D('User')->getById($vo['user_id']);
			if($user)
				$this->assign("user",$user);
		}

		$citys = D("GroupCity")->where("status = 1")->order("sort asc,id desc,py asc")->findAll();
		$this->assign("citys",$citys);

		$ruleType = D("CollectRuleType")->where("status = 1")->order("sort asc,id desc")->findAll();
		$this->assign("ruleType",$ruleType);

		//导航分类
		$cateList = D("SiteCate")->getSiteCateList();
		$this->assign("cateList",$cateList);
		$this->display();
	}

	public function update()
	{
		$model = D(MODULE_NAME);
		$_POST['content'] = addslashes($_POST['content']);
		$_POST['is_all'] = isset($_POST['is_all']) ? intval($_POST['is_all']) : 0;

		$id = $_REQUEST[$model->getPk()];
		$ole_name = D("Site")->where("id=$id")->getField("name");
		$new_name = $_REQUEST['name'];

		if (false === $model->create())
		{
			$this->error($model->getError());
		}

		// 更新数据
		$bln = $model->save();

		if (false !== $bln)
		{
			if($ole_name != $new_name)
			{
				D("Goods")->where("site_id=$id")->setField("site_name",$new_name);
				D("GoodsNow")->where("site_id=$id")->setField("site_name",$new_name);
			}

			$upload_list = $this->uploadFile(1,"site");
			if($upload_list)
			{
				$site_img = D("Site")->where("id=$id")->getField("site_img");
				unlink($this->getRealPath().$site_img);
				D("Site")->where("id=$id")->setField("site_img",$upload_list[0]['recpath'].$upload_list[0]['savename']);
			}

			D("SiteRules")->where("id=$id")->delete();

			$data = array();
			$data['collect_url'] = trim($_POST['collect_url']);
			$data['collect_code'] = trim($_POST['collect_code']);
			$data['collect_type'] = intval($_POST['collect_type']);
			$data['web_type'] = intval($_POST['web_type']);
			$data['collect_city_api'] = trim($_POST['collect_city_api']);

			if($data['collect_type'] == 1)
			{
				foreach($_POST['web'] as $key => $val)
				{
					$data[$key] = $val;
				}
			}
			else
			{
				foreach($_POST['api'] as $key => $val)
				{
					$data[$key] = $val;
				}
			}

			foreach($data as $key => $value)
			{
				if(!empty($value))
				{
					$site_rules = array();
					$site_rules['id'] = $id;
					$site_rules['name'] = $key;
					$site_rules['rule'] = $value;
					D("SiteRules")->add($site_rules);
				}
			}

			D('CitySite')->where('site_id = '.$id)->delete();
			$city_site=$_POST['city_site'];
			foreach($city_site as $k=>$v)
			{
				$city_site_list=array();
				$city_site_list['site_id']=$id;
				$city_site_list['city_id']=$k;
				$city_site_list['sort']=$v;

				if(is_numeric($v))
				{
					D('CitySite')->add($city_site_list);
				}
			}
			//成功提示
			$this->saveLog(1);
			$this->success(L('EDIT_SUCCESS'));
		}
		else
		{
			//错误提示
			$this->saveLog(0);
			$this->error(L('EDIT_FAILED'));
		}
	}

	public function foreverdelete ()
	{
		//删除指定记录
		$model = D(MODULE_NAME);
		if (!empty($model))
		{
			$pk = $model->getPk();
			$id = $_REQUEST[$pk];
			$ids = explode ( ',', $id );
			if (count($ids) > 0)
			{
				$names = '';
				$images = array();
				foreach($ids as $id)
				{
					$site = M("Site")->where("id=".$id)->field("name,site_img")->find();
					$names .= $site['name'].",";
					$images[] = $site['site_img'];
				}

				if($names!='')
				{
					$names = substr($names,0,strlen($names)-1);
				}

				$condition = array($pk => array('in',$ids));
				$msg = L('DELETE_MSG').$names;
				if (false !== $model->where($condition)->delete())
				{
					D("SiteRules")->where($condition)->delete();

					foreach($images as $img)
					{
						unlink($this->getRealPath().$img);
					}
					$this->saveLog(1,0,$msg);
					$this->success (L('DEL_SUCCESS'));
				}
				else
				{
					$this->saveLog(0,0,$msg);
					$this->error(L('DEL_FAILED'));
				}
			}
			else
			{
				$this->saveLog(0);
				$this->error(L('INVALID_OP'));
			}
		}
		$this->forward();
	}

	public function getSiteLinks($id)
	{
		$url = __ROOT__."/services/admin.php?action=updatesite&id=".$id;
		return ' <a href="'.$url.'" target="_blank">更新站点排序</a>';
	}

	public function checkKey()
	{
		$key = $_REQUEST['key'];

		if(D(MODULE_NAME)->checkKey($key))
			echo 1;
		else
			echo 0;
	}

	//搜索站点
	public function getSites()
	{
		$sites = array();
		$key = trim($_REQUEST['key']);
		if(!empty($key))
		{
			$where['name'] = array("like","%$key%");
			$where['site_key'] = array("like","%$key%");
			$where['url'] = array("like","%$key%");
			$where['_logic'] = 'or';
			$condition['_complex'] = $where;

			$sites = D(MODULE_NAME)->where($condition)->field("id,name")->order("sort asc,id asc")->limit(30)->select();
		}
		else
		{
			$sites = D(MODULE_NAME)->where($condition)->field("id,name")->order("sort asc,id asc")->limit(30)->select();
		}

		echo json_encode($sites);
	}

	//修改推荐状态
	public function updateIsBest()
	{
		$id = intval($_REQUEST['id']);
		$isBest = D(MODULE_NAME)->where('id='.$id)->getField("is_best");

		if($isBest == 0)
			$isBest = 1;
		else
			$isBest = 0;

		if(D(MODULE_NAME)->where('id='.$id)->setField("is_best",$isBest))
			echo $isBest;
		else
			echo "err";
	}
	//修改推荐状态
	public function updateIsAll()
	{
		$id = intval($_REQUEST['id']);
		$is_all = D(MODULE_NAME)->where('id='.$id)->getField("is_all");

		if($is_all == 0)
			$is_all = 1;
		else
			$is_all = 0;

		if(D(MODULE_NAME)->where('id='.$id)->setField("is_all",$is_all))
			echo $is_all;
		else
			echo "err";
	}

	//修改网站排序
	public function updateSiteSort()
	{
		$id = intval($_REQUEST['id']);
		$sort = intval($_REQUEST['sort']);
		if(D(MODULE_NAME)->where('id='.$id)->setField("sort",$sort))
			echo 1;
		else
			echo 0;
	}
	//修改网站排序
	public function updateSite_GoodsSort()
	{
		$id = intval($_REQUEST['id']);
		$sort = intval($_REQUEST['sort']);
		if(D(MODULE_NAME)->where('id='.$id)->setField("goods_sort",$sort))
			echo 1;
		else
			echo 0;
	}

	//修改推荐状态
	public function updateIsAuth()
	{
		$id = intval($_REQUEST['id']);
		$is_auth = D(MODULE_NAME)->where('id='.$id)->getField("is_auth");

		if($is_auth == 0)
			$is_auth = 1;
		else
			$is_auth = 0;

		if(D(MODULE_NAME)->where('id='.$id)->setField("is_auth",$is_auth))
			echo $is_auth;
		else
			echo "err";
	}
	public function getGroupCityList_by_py($site_id)
	{
		$sql='SELECT gc.id,gc.name,gc.py_t,IFNULL(cs.sort,\'\') AS sort,IF(cs.site_id IS NOT NULL,1,0) AS is_city
				FROM '.C('DB_PREFIX').'group_city AS gc
				LEFT JOIN '.C('DB_PREFIX').'city_site AS cs ON cs.city_id = gc.id AND cs.site_id = '.$site_id.'
				WHERE gc.status = 1 AND gc.py <> \'china\' GROUP BY gc.id ORDER BY gc.sort ASC,gc.id ASC';

		$city_list = M()->query($sql);
		$citys=array();
		foreach($city_list as $k=>$v)
		{
			$v['py_t'] = strtoupper($v['py_t']);

			if($v['py_t'] >= 'A' && $v['py_t'] <= 'G')
				$citys['A-G'][] = $v;

			if($v['py_t'] >= 'H' && $v['py_t'] <= 'N')
				$citys['H-N'][] = $v;

			if($v['py_t'] >= 'O' && $v['py_t'] <= 'T')
				$citys['O-T'][] = $v;

			if($v['py_t'] >= 'U' && $v['py_t'] <= 'Z')
				$citys['U-Z'][] = $v;
		}

		ksort($citys);

		$this->assign("site_citys",$citys);
	}
}
?>